#pragma once

#include <ConnectionBroker/server/Server.h>
#include <ConnectionBroker/clientconnection/LongPollClientConnection.h>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>

namespace ConnectionBroker
{
   typedef boost::shared_ptr<LongPollServer> LongPollServerPtr;
   class LongPollConnection;
   class LongPollServer : public ConnectionBroker::Server
   {
      protected: //Member variables
      boost::asio::io_service mIoService;
      boost::asio::ip::tcp::acceptor mAcceptor;
      boost::shared_ptr<ConnectionBroker::LongPollConnection> mNextConnection;
      std::map<std::pair<int, std::string>, ConnectionBroker::LongPollClientConnectionPtr> mIdPassClientMap;
      int mNextClientId;
      int mNextClientPass;

      public: //Constructor, destructor
      LongPollServer(const ConnectionBroker::ServerSettings& settings, 
                serverCallback pAppCallback, 
                serverCallback pAppClientDisconnectCallback,
                serverCallback pAppClientConnectCallback);
      virtual ~LongPollServer();

   public: //Interface
   void start();
   ConnectionBroker::LongPollClientConnectionPtr longPollClientFactory();
   ConnectionBroker::LongPollClientConnectionPtr idPassToClient(const int id, const std::string& pass);
   void longPollClientRemove(ConnectionBroker::LongPollClientConnectionPtr pClient);

   protected: //Utilities
   std::string generatePassword();
   void handleAccept(const boost::system::error_code& error);
   void startAcceptingClients();

   };
}

